// SystemConfig.cpp : implementation file
//

#include "stdafx.h"
#include "../Frame/GYWalkAidApp.h"
#include "SystemConfig.h"
#include "../Common/GYDataSet.h"
#include "../Common/CommonFun.h"
#include "../Common/CommonDef.h"
#include "../Frame/NavigateDlg.h"
#include <io.h>
#include "../Common/ColorCombo.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

static struct 
{
	DWORD			chkID;
	DWORD			dwColorID;
	int				nDevIndex;
	BOOL			bShowColor;
	int				nColorIndex;
	DWORD			aBattery[MAX_SUPPORT_WA_COUNT];
}
s_Contrls[] =
{
	{IDC_CHECK1,	IDC_STATIC_COLOR1,	-1,	FALSE,	0,	{IDC_STATIC_SBATTERY00,	IDC_STATIC_SBATTERY01,	IDC_STATIC_SBATTERY02,	IDC_STATIC_SBATTERY03,	IDC_STATIC_SBATTERY04 }}, 
	
	{IDC_CHECK2,	IDC_STATIC_COLOR2,	-1,	FALSE,	0,	{IDC_STATIC_SBATTERY10,	IDC_STATIC_SBATTERY11,	IDC_STATIC_SBATTERY12,	IDC_STATIC_SBATTERY13,	IDC_STATIC_SBATTERY14 }}, 
	
	{IDC_CHECK3,	IDC_STATIC_COLOR3,	-1,	FALSE,	0,	{IDC_STATIC_SBATTERY20,	IDC_STATIC_SBATTERY21,	IDC_STATIC_SBATTERY22,	IDC_STATIC_SBATTERY23,	IDC_STATIC_SBATTERY24 }}, 
	
	{IDC_CHECK4,	IDC_STATIC_COLOR4,	-1,	FALSE,	0,	{IDC_STATIC_SBATTERY30,	IDC_STATIC_SBATTERY31,	IDC_STATIC_SBATTERY32,	IDC_STATIC_SBATTERY33,	IDC_STATIC_SBATTERY34 }}, 
	
	{IDC_CHECK5,	IDC_STATIC_COLOR5,	-1,	FALSE,	0,	{IDC_STATIC_SBATTERY40,	IDC_STATIC_SBATTERY41,	IDC_STATIC_SBATTERY42,	IDC_STATIC_SBATTERY43,	IDC_STATIC_SBATTERY44 }}, 
};

static struct
{
	DWORD			dwButtonId;
	BOOL			bEnable;
}
s_ButtonsEnable[] =
{
	{IDC_BUTTON_DATA_BACKUP,		FALSE},
	{IDC_BUTTON_SYS_UPGRADE,		FALSE},
	{IDC_BUTTON_SET_PASSWORD,		FALSE},
	{IDC_BUTTON_NEW_USER,			FALSE},
	{IDC_BUTTON_SAVE_WADATA,		FALSE},
	{IDC_BUTTON_WRITE_WA,			FALSE},
	{IDC_BUTTON_SYS_RETURN,			FALSE},
};


/////////////////////////////////////////////////////////////////////////////
// CSystemConfig dialog


CSystemConfig::CSystemConfig(CWnd* pParent /*=NULL*/)
	: CDialog(CSystemConfig::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSystemConfig)
	m_strSetOldPwd = _T("");
	m_strSetNewPwd = _T("");
	m_strSetNewConfirm = _T("");
	m_strNewUserName = _T("");
	m_strNewPassword = _T("");
	m_strNewPasswordConfirm = _T("");
	m_strNewRealName = _T("");
	//}}AFX_DATA_INIT

	memset(&m_wbpCurWA, 0, sizeof(m_wbpCurWA));
	memset(&m_wbpCurWA4Time, 0, sizeof(m_wbpCurWA4Time));

	m_nCurrentWA = 0;
	m_nCurrentWA4Time = 0;
}


void CSystemConfig::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSystemConfig)
	DDX_Control(pDX, IDC_EDIT_SYS_DOWNTIME, m_edtDownTime);
	DDX_Control(pDX, IDC_EDIT_SYS_UPTIME, m_edtUpTime);
	DDX_Text(pDX, IDC_EDIT_SET_OLD_PWD, m_strSetOldPwd);
	DDX_Text(pDX, IDC_EDIT_SET_NEW_PWD, m_strSetNewPwd);
	DDX_Text(pDX, IDC_EDIT_SET_NEW_CONFIRM, m_strSetNewConfirm);
	DDX_Text(pDX, IDC_EDIT_SYS_NEW_USERNAME, m_strNewUserName);
	DDX_Text(pDX, IDC_EDIT_SYS_NEW_PASSWORD, m_strNewPassword);
	DDX_Text(pDX, IDC_EDIT_SYS_NEW_CONFIRM, m_strNewPasswordConfirm);
	DDX_Text(pDX, IDC_EDIT_SYS_NEW_REALNAME, m_strNewRealName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSystemConfig, CDialog)
	//{{AFX_MSG_MAP(CSystemConfig)
	ON_BN_CLICKED(IDC_BUTTON_SYS_RETURN, OnButtonSysReturn)
	ON_BN_CLICKED(IDC_BUTTON_SET_PASSWORD, OnButtonSetPassword)
	ON_BN_CLICKED(IDC_BUTTON_NEW_USER, OnButtonNewUser)
	ON_BN_CLICKED(IDC_BUTTON_WRITE_WA, OnButtonWriteWAParam)
	ON_BN_CLICKED(IDC_BUTTON_SAVE_WADATA, OnButtonSaveHistoryData)
	ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
	ON_BN_CLICKED(IDC_CHECK2, OnCheck2)
	ON_BN_CLICKED(IDC_CHECK3, OnCheck3)
	ON_BN_CLICKED(IDC_CHECK4, OnCheck4)
	ON_BN_CLICKED(IDC_CHECK5, OnCheck5)
	ON_CBN_SELCHANGE(IDC_COMBO_SYS_USERS, OnSelchangeComboSysUsers)
	ON_CBN_SELCHANGE(IDC_COMBO_WADEVICES, OnSelchangeWAList)
	ON_BN_CLICKED(IDC_BUTTON_DATA_BACKUP, OnButtonDataBackup)
	ON_BN_CLICKED(IDC_BUTTON_SYS_UPGRADE, OnButtonSysUpgrade)
	ON_CBN_SELCHANGE(IDC_COMBO_WADEVICES_TIME, OnSelchangeComboWadevicesTime)
	ON_BN_CLICKED(IDC_BUTTON_WRITE_WATIME, OnButtonWriteWaTime)
	ON_BN_CLICKED(IDC_BUTTON_SET_HOSPITAL_NAME, OnButtonSetHospitalName)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSystemConfig message handlers

void CSystemConfig::OnButtonSysReturn() 
{
	CDialog::OnCancel();
}

BOOL CSystemConfig::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_edtUpTime.SetMask(_T("#.#"));
    m_edtUpTime.SetPromptSymbol(_T(' '));
	
	m_edtDownTime.SetMask(_T("#.#"));
    m_edtDownTime.SetPromptSymbol(_T(' '));

	for(int i = 0; i < MAX_SUPPORT_WA_COUNT; i++)
	{
		COLORREF crWAColor = CColorComboBox::GetWAColor(i);
		m_WAColor[i].CreateSolidBrush(crWAColor);
	}	

	//  Init hospital name
	ReadHospitalName();

	//  Init user
	ReadUserInfo();

	//  Init WA list
	ShowSaveDataWAList();

	//  Set text length
	SetEditLimit();

	//  Init WA ComboBox List
	InitWAParamCombo();

	// Init WA list to set key param
	ShowListToSetParam();

	//  Set privilege
	switch(GetApp()->GetUserRoleID())
	{
	case USER_ROLE_COMMON_DOCTOR:
		EnableSetPassword();
		DisableBackup();
		DisableSystemUpgrade();
		DisableNewUser();
//		DisableSetWAParam4Time();
		DisableSaveWAData();
		DisableSetHospitalName();
		DisableSetWAParam();

	case USER_ROLE_HOSPITAL_ADMIN:
		DisableSystemUpgrade();

	case USER_ROLE_GY_ADMIN:
		break;
	}
	
	InitWAColorIndex();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSystemConfig::ReadHospitalName()
{
	CString strSQL;
	strSQL.Format(_T("SELECT ComputerNo FROM SysConfig WHERE ConfigName='SoftwareSerialNo' AND ConfigValue='%s'"), GetApp()->GetSoftwareSN());
	CGYDataSet setSysConfig(strSQL);
	
	setSysConfig.GetFieldValue(_T("ComputerNo"), m_strComputerNo);
	
	strSQL.Format(_T("SELECT ConfigValue FROM SysConfig WHERE ComputerNo=%s AND ConfigName='HospitalName'"), m_strComputerNo);
	setSysConfig.OpenTable(strSQL);

	CEdit* pHptName = (CEdit*)GetDlgItem(IDC_EDIT_SYS_HOSPITAL_NAME);
	ASSERT(pHptName);

	if(setSysConfig.IsEOF())
	{
		pHptName->SetWindowText(GetResString(IDS_SYS_A_HOSPITAL));
	}
	else
	{
		setSysConfig.GetFieldValue(_T("ConfigValue"), m_strHospitalName);
		pHptName->SetWindowText(m_strHospitalName);
	}

#ifdef _UNICODE
	pHptName->SetLimitText(30);
#else
	pHptName->SetLimitText(60);
#endif
}

void CSystemConfig::ReadUserInfo()
{
	CComboBox* pComboUsers = (CComboBox*)GetDlgItem(IDC_COMBO_SYS_USERS);
	pComboUsers->ResetContent();

	m_vecUserInfo.clear();

	CGYDataSet setUsers;
	CString strSQL;
	strSQL.Format(_T("SELECT UserName, RealName, UserPassword FROM UserInfo WHERE RoleID<=%d ORDER BY UserName"), GetApp()->GetUserRoleID());
	
	int i = 0;
	int nCurUserIndex = 0;
	for(setUsers.OpenTable(strSQL); !setUsers.IsEOF(); setUsers.MoveNext())
	{
		USERINFO uInfo;

		setUsers.GetFieldValue(_T("UserName"),		uInfo.strUserName);
		setUsers.GetFieldValue(_T("RealName"),		uInfo.strRealName);
		setUsers.GetFieldValue(_T("UserPassword"),	uInfo.strPassword);
		
		pComboUsers->AddString(uInfo.strUserName);
		m_vecUserInfo.push_back(uInfo);


		if(uInfo.strUserName.CompareNoCase(GetApp()->GetUserName()) == 0)
			nCurUserIndex = i;

		i++;
	}

	if(GetApp()->GetUserRoleID() == USER_ROLE_COMMON_DOCTOR)
	{
		pComboUsers->SetCurSel(nCurUserIndex);
		GetDlgItem(IDC_STATIC_SET_REALNAME)->SetWindowText(m_vecUserInfo[nCurUserIndex].strRealName);
		pComboUsers->EnableWindow(FALSE);
	}
	else
	{
		pComboUsers->SetCurSel(0);
		GetDlgItem(IDC_STATIC_SET_REALNAME)->SetWindowText(m_vecUserInfo[0].strRealName);
	}

}

void CSystemConfig::ShowSaveDataWAList()
{
	CWAManager* pBTMan = GetApp()->GetWAMan();
	int nWACount = pBTMan->GetWADeviceCount();
	int nWAIndex = pBTMan->GetFirstDevIndex();

	for(int i = 0; i < ARRAY_ELE_COUNT(s_Contrls); i++)
	{
		DWORD	dwCommVer		= (i < nWACount						)	? pBTMan->GetWACommVersion(nWAIndex)	: 0;
		CString	strDevName		= (i < nWACount						)	? pBTMan->GetWADevName(nWAIndex)		: _T("99");
		CString	strDevID		= (i < nWACount						)	? pBTMan->GetWADevID(nWAIndex)			: _T(""); 
		DWORD	dwBattery		= (i < nWACount && dwCommVer >= 0x12)	? pBTMan->cmdGetBattery(nWAIndex)		: 0xff;
		BOOL	bShowColor		= (i < nWACount && dwCommVer >= 0x12);
		
		CWnd* pCheck = GetDlgItem(s_Contrls[i].chkID);
		ASSERT(pCheck);
		CWnd* pColor = GetDlgItem(s_Contrls[i].dwColorID);
		ASSERT(pColor);

		pCheck->SetWindowText	(strDevID);
		pCheck->EnableWindow	(dwCommVer >= 0x12);
		pCheck->ShowWindow		(i < nWACount	? SW_SHOW	: SW_HIDE);
		pColor->ShowWindow		(bShowColor		? SW_SHOW : SW_HIDE);

		s_Contrls[i].bShowColor	= bShowColor;

		ShowWABattery(i, dwBattery);

		s_Contrls[i].nDevIndex = nWAIndex;

		if(nWAIndex >= 0)
			nWAIndex = pBTMan->GetNextDevIndex(nWAIndex);
	}
}

void CSystemConfig::ShowWABattery(int nGroup, DWORD dwBattery)
{
	for(int j = 0; j < 5; j++)
	{
		CWnd* pBattery = GetDlgItem(s_Contrls[nGroup].aBattery[j]);
		ASSERT(pBattery);

		pBattery->ShowWindow(j == dwBattery ? SW_SHOW : SW_HIDE);
	}
}

void CSystemConfig::ShowListToSetParam()
{
	CComboBox* pComboWA = (CComboBox*)GetDlgItem(IDC_COMBO_WADEVICES);
	ASSERT(pComboWA);

	CComboBox* pComboWA4Time = (CComboBox*)GetDlgItem(IDC_COMBO_WADEVICES_TIME);
	ASSERT(pComboWA4Time);

	pComboWA->ResetContent();
	m_vecWAInfo.clear();
	m_vecWAInfo4Time.clear();
	
	CWAManager* pBTMan = GetApp()->GetWAMan();
	int nWACount = pBTMan->GetWADeviceCount();
	int nWAIndex = pBTMan->GetFirstDevIndex();

	for(int i = 0; i < nWACount; i++)
	{
		WAINFO wainfo;
		wainfo.nWAIndex = nWAIndex;
		wainfo.strDevID = pBTMan->GetWADevID(nWAIndex);

		pComboWA4Time->AddString(wainfo.strDevID);
		m_vecWAInfo4Time.push_back(wainfo);

		DWORD	dwCommVer = pBTMan->GetWACommVersion(nWAIndex);
		if(dwCommVer >= 0x12)
		{
			pComboWA->AddString(wainfo.strDevID);
			m_vecWAInfo.push_back(wainfo);
		}

		nWAIndex = pBTMan->GetNextDevIndex(nWAIndex);
	}

	BOOL bTimeWAComboInited = FALSE;
	if(pComboWA->GetCount() > 0)
	{
		int nWAInex = m_vecWAInfo[0].nWAIndex;
		m_wbpCurWA = pBTMan->cmdGetBasicPara(nWAInex);
		if(m_wbpCurWA.dwSize == 0)
		{
			GYMsgBox(IDS_SYS_READ_PARAM_ERROR, MB_ICONSTOP);
			return;
		}

		pComboWA->SetCurSel(0);
		m_nCurrentWA = 0;

		if(m_wbpCurWA.dwPulseFrq < 3 || m_wbpCurWA.dwPulseFrq > 6)
		{
			if(m_wbpCurWA.dwPulseFrq == 0)
				m_wbpCurWA.dwPulseFrq = 1;

			CString strFormat = GetResString(IDS_SYS_PULSE_FREQ_WRONG);
			CString strInfo;
			strInfo.Format(strFormat, 100.0/m_wbpCurWA.dwPulseFrq);
			GYMsgBox(strInfo, MB_ICONWARNING);
		}

		if(m_wbpCurWA.dwPulseWidth < 1 || m_wbpCurWA.dwPulseWidth > 5)
		{
			CString strFormat = GetResString(IDS_SYS_PULSE_WIDTH_WRONG);
			CString strInfo;
			strInfo.Format(strFormat, m_wbpCurWA.dwPulseWidth * 50);
			GYMsgBox(strInfo, MB_ICONWARNING);
		}
		
		CString strParam;

		CComboBox* pComboFreq = (CComboBox*)GetDlgItem(IDC_COMBO_STI_FREQ);
		pComboFreq->SetCurSel(6 - m_wbpCurWA.dwPulseFrq);
		
		CComboBox* pComboWidth = (CComboBox*)GetDlgItem(IDC_COMBO_STI_WIDTH);
		pComboWidth->SetCurSel(m_wbpCurWA.dwPulseWidth - 1);
			
		CWnd* pStiCount = GetDlgItem(IDC_EDIT_SYS_SAFE_PULSENUM);
		strParam.Format(_T("%d"), m_wbpCurWA.dwPulseCount);
		pStiCount->SetWindowText(strParam);

		if(m_vecWAInfo[0].nWAIndex == m_vecWAInfo4Time[0].nWAIndex)
		{
			m_wbpCurWA4Time = m_wbpCurWA;

			pComboWA4Time->SetCurSel(0);
			m_nCurrentWA4Time = 0;

			CString strUpTime;
			strUpTime.Format(_T("%.1f"), m_wbpCurWA4Time.dwStiUpTime / 100.0);
			GetDlgItem(IDC_EDIT_SYS_UPTIME)->SetWindowText(strUpTime);
			
			CString strDownTime;
			strDownTime.Format(_T("%.1f"), m_wbpCurWA4Time.dwStiDownTime / 100.0);
			GetDlgItem(IDC_EDIT_SYS_DOWNTIME)->SetWindowText(strDownTime);

			bTimeWAComboInited = TRUE;
		}
	}
	else
	{
		pComboWA->AddString(GetResString(IDS_SYS_NO_WA_II));
		pComboWA->SetCurSel(0);
		pComboWA->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_WRITE_WA)->EnableWindow(FALSE);
	}

	if(!bTimeWAComboInited && pComboWA4Time->GetCount() > 0)
	{
		pComboWA4Time->SetCurSel(0);
		m_nCurrentWA4Time = 0;

		// make BT not so busy
		Sleep(200);

		int nWAInex = m_vecWAInfo4Time[0].nWAIndex;
		m_wbpCurWA4Time = pBTMan->cmdGetBasicPara(nWAInex);

		if(m_wbpCurWA4Time.dwSize == 0)
		{
			GYMsgBox(IDS_SYS_READ_PARAM_ERROR, MB_ICONSTOP);
			return;
		}

		CString strUpTime;
		strUpTime.Format(_T("%.1f"), m_wbpCurWA4Time.dwStiUpTime / 100.0);
		GetDlgItem(IDC_EDIT_SYS_UPTIME)->SetWindowText(strUpTime);
		
		CString strDownTime;
		strDownTime.Format(_T("%.1f"), m_wbpCurWA4Time.dwStiDownTime / 100.0);
		GetDlgItem(IDC_EDIT_SYS_DOWNTIME)->SetWindowText(strDownTime);
	}
}

void CSystemConfig::InitWAParamCombo()
{
	CComboBox* pComboFreq = (CComboBox*)GetDlgItem(IDC_COMBO_STI_FREQ);
	pComboFreq->AddString(_T("16.7"));
	pComboFreq->AddString(_T("20"));
	pComboFreq->AddString(_T("25"));
	pComboFreq->AddString(_T("33"));

	CComboBox* pComboWidth = (CComboBox*)GetDlgItem(IDC_COMBO_STI_WIDTH);
	pComboWidth->AddString(_T("50"));
	pComboWidth->AddString(_T("100"));
	pComboWidth->AddString(_T("150"));
	pComboWidth->AddString(_T("200"));
	pComboWidth->AddString(_T("250"));
}

void CSystemConfig::OnButtonSetPassword() 
{
	UpdateData();

	CComboBox* pComboUsers = (CComboBox*)GetDlgItem(IDC_COMBO_SYS_USERS);
	int nCurSel = pComboUsers->GetCurSel();
	
	m_strSetOldPwd.TrimLeft();
	m_strSetOldPwd.TrimRight();
	
	m_strSetNewPwd.TrimLeft();
	m_strSetNewPwd.TrimRight();
	
	m_strSetNewConfirm.TrimLeft();
	m_strSetNewConfirm.TrimRight();

	BOOL bCommonUser = (GetApp()->GetUserRoleID() == USER_ROLE_COMMON_DOCTOR);

	if(	m_strSetNewPwd.IsEmpty()					||
		m_strSetNewConfirm.IsEmpty()				||
		(bCommonUser && m_strSetOldPwd.IsEmpty())	)
	{
		GYMsgBox(IDS_USER_PWD_EMPTY, MB_OK|MB_ICONSTOP);
		return;
	}

	CString strEncryptOldPwd = EncryptString(m_strSetOldPwd);
	if(bCommonUser && strEncryptOldPwd != m_vecUserInfo[nCurSel].strPassword)
	{
		GYMsgBox(IDS_SYS_OLDPWD_WRONG, MB_OK|MB_ICONSTOP);
		return;
	}
	
	if(m_strSetNewPwd != m_strSetNewConfirm)
	{
		GYMsgBox(IDS_SYS_OLD_NEW_NOTSAME, MB_OK|MB_ICONSTOP);
		return;
	}

	CString strUserName;
	pComboUsers->GetLBText(nCurSel, strUserName);

	CString strNewEncryptPassword = EncryptString(m_strSetNewPwd);

	CGYDataSet tblUser;
	CString strSQL;
	strSQL.Format(_T("UPDATE UserInfo SET UserPassword='%s' WHERE UserName='%s'"), strNewEncryptPassword, strUserName);
	tblUser.ExecuteSQL(strSQL);
	
	m_vecUserInfo[nCurSel].strPassword = strNewEncryptPassword;
	
	GYMsgBox(IDS_SYS_PWD_CHANGE_OK, MB_OK|MB_ICONINFORMATION);

	m_strSetOldPwd.Empty();
	m_strSetNewPwd.Empty();
	m_strSetNewConfirm.Empty();
	UpdateData(FALSE);
}

void CSystemConfig::OnButtonNewUser() 
{
	UpdateData();

	m_strNewUserName.TrimLeft();
	m_strNewUserName.TrimRight();
	m_strNewRealName.TrimLeft();
	m_strNewRealName.TrimRight();
	m_strNewPassword.TrimLeft();
	m_strNewPassword.TrimRight();
	m_strNewPasswordConfirm.TrimLeft();
	m_strNewPasswordConfirm.TrimRight();
	
	if(m_strNewUserName.IsEmpty() || m_strNewPassword.IsEmpty() || m_strNewPasswordConfirm.IsEmpty())
	{
		GYMsgBox(IDS_USER_PWD_EMPTY, MB_OK|MB_ICONSTOP);
		return;
	}
	if(m_strNewRealName.IsEmpty())
	{
		GYMsgBox(IDS_SYS_REALNAME_EMPTY, MB_OK|MB_ICONSTOP);
		return;
	}

	if(m_strNewPassword != m_strNewPasswordConfirm)
	{
		GYMsgBox(IDS_SYS_OLD_NEW_NOTSAME, MB_OK|MB_ICONSTOP);
		return;
	}

	CString strSafeUserName = m_strNewUserName;
	strSafeUserName.Replace(_T("'"), _T("''"));

	CString strSafeRealName = m_strNewRealName;
	strSafeRealName.Replace(_T("'"), _T("''"));

	CGYDataSet tblUser;
	CString strSQL;
	strSQL.Format(_T("SELECT UserName FROM UserInfo WHERE UserName='%s'"), strSafeUserName);
	tblUser.OpenTable(strSQL);
	if(!tblUser.IsEOF())
	{
		GYMsgBox(IDS_SYS_USER_EXIST, MB_OK|MB_ICONSTOP);
		return;
	}

	CString strEncryptPassword = EncryptString(m_strNewPassword);
	strSQL.Format(_T("INSERT INTO UserInfo (UserName, RealName, RoleID, UserPassword) VALUES ('%s', '%s', 0, '%s')"), strSafeUserName, strSafeRealName, strEncryptPassword);
	tblUser.ExecuteSQL(strSQL);

	GYMsgBox(IDS_SYS_NEW_USER_SUCC, MB_OK|MB_ICONINFORMATION);
	m_strNewUserName.Empty();
	m_strNewRealName.Empty();
	m_strNewPassword.Empty();
	m_strNewPasswordConfirm.Empty();

	UpdateData(FALSE);
	ReadUserInfo();
}

void CSystemConfig::OnButtonSaveHistoryData()
{
// 	CProgressCtrl* pProgress = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS_SAVE_WADATA);
// 	ASSERT(pProgress);
// 	pProgress->SetRange(0, 100);

	CWaitCursor wait;
	GetDlgItem(IDC_STATIC_READING_HISTDATA)->ShowWindow(SW_SHOW);

	int nPos = 0;
	CWAManager* pBTMan = GetApp()->GetWAMan();

	for(int j = 0; j < ARRAY_ELE_COUNT(s_ButtonsEnable); j++)
	{
		//  remember buttons enable state
		s_ButtonsEnable[j].bEnable = GetDlgItem(s_ButtonsEnable[j].dwButtonId)->IsWindowEnabled();

		GetDlgItem(s_ButtonsEnable[j].dwButtonId)->EnableWindow(FALSE);
	}

	for(int i = 0; i < ARRAY_ELE_COUNT(s_Contrls); i++)
	{
		CButton* pCheck = (CButton*)GetDlgItem(s_Contrls[i].chkID);
		if(pCheck->GetCheck())
		{
			int nWAIndex = s_Contrls[i].nDevIndex;
			if(nWAIndex != -1)
			{
				BOOL bSavOK = TRUE;

				if(pBTMan->cmdStartReadHistory(nWAIndex))
				{
					CGYDataSet setHistory;
					while(1)
					{
						WA_HISTORY_DATA whd = pBTMan->cmdReadHistoryData(nWAIndex);
						if(whd.dwSize == 0)
						{
							if(pBTMan->GetLastWAError(nWAIndex) == WA_ERROR_TIMEOUT || pBTMan->GetLastWAError(nWAIndex) == WA_ERROR_NOTCONNECT)
								break;
						}

						CString strTime;
						strTime = whd.tTime.Format(_T("%Y-%m-%d %H:%M:%S"));

						CString strSQL;
						strSQL.Format(_T("INSERT INTO WAHistoryData (MRNCode, UseTime, TimeSpan, StepCount) VALUES (%d, '%s', %d, %d)"), 
										whd.dwMRNCode, strTime, whd.dwTimespan, whd.dwStepCount);

						if(!setHistory.ExecuteSQL(strSQL))
						{
							bSavOK = FALSE;
							DataErrorMsgBox(__FILE__, __LINE__, strSQL, setHistory.GetLastErrorString());
							break;
						}

						nPos++;
						nPos = (nPos % 100);
//						pProgress->SetPos(nPos);
					}
				}

				//  write time to WA
				CTime tCur = CTime::GetCurrentTime();
				if(tCur.GetYear() < 2000)
				{
					GYMsgBox(IDS_SYS_CUR_DATE_ERROR, MB_ICONERROR|MB_OK);
					return;
				}

				WA_BASIC_PARA wbp = {0};

				wbp.dwYear		= tCur.GetYear() - 2000;
				wbp.dwMonth		= tCur.GetMonth();
				wbp.dwDay		= tCur.GetDay();
				wbp.dwWeek		= tCur.GetDayOfWeek() == 1 ? 7 : (tCur.GetDayOfWeek() - 1);
				wbp.dwHour		= tCur.GetHour();
				wbp.dwMinute	= tCur.GetMinute();

				//  Maybe some bytes come from gait, sleep some time
				Sleep(300);

				BOOL bWriteOK = FALSE;
				WRITE_WA_MORE_TIMES(bWriteOK, pBTMan->cmdWriteWATime(nWAIndex, wbp));

				if(!bWriteOK)
				{
					GYMsgBox(IDS_SYS_WRITE_TIME_ERROR, MB_OK|MB_ICONWARNING);
				}

				if(bSavOK)
					pBTMan->cmdClearHistoryData(nWAIndex);
			}
		}
	}


	GetDlgItem(IDC_STATIC_READING_HISTDATA)->ShowWindow(SW_HIDE);

	GYMsgBox(IDS_SYS_READ_HISTORY_COMPLETE, MB_ICONINFORMATION);
//	pProgress->SetPos(0);

	//  restore buttons enable state
	for(int k = 0; k < ARRAY_ELE_COUNT(s_ButtonsEnable); k++)
		GetDlgItem(s_ButtonsEnable[k].dwButtonId)->EnableWindow(s_ButtonsEnable[k].bEnable);
}

void CSystemConfig::OnButtonWriteWAParam()
{
	CWaitCursor wait;

	CWAManager* pBTMan = GetApp()->GetWAMan();
	CComboBox* pComboWA = (CComboBox*)GetDlgItem(IDC_COMBO_WADEVICES);
	
	if(pComboWA && pComboWA->GetCount() > 0)
	{
		CWnd* pStiCount = GetDlgItem(IDC_EDIT_SYS_SAFE_PULSENUM);
		CString strParam;
		pStiCount->GetWindowText(strParam);
		DWORD dwPulseCount = _ttoi(strParam);


		if(GetApp()->GetUserRoleID() != USER_ROLE_GY_ADMIN)
		{
			if(dwPulseCount < 17 || dwPulseCount > 25)
			{
				GYMsgBox(IDS_SYS_PULSE_17_25, MB_ICONSTOP);
				return;
			}
		}

		if(dwPulseCount < 0 || dwPulseCount > 255)
		{
			GYMsgBox(IDS_SYS_PULSE_COUNT_ERROR, MB_ICONSTOP);
			return;
		}

		int nWASel = pComboWA->GetCurSel();
		int nWAIndex = m_vecWAInfo[nWASel].nWAIndex;

		CComboBox* pComboFreq = (CComboBox*)GetDlgItem(IDC_COMBO_STI_FREQ);
		int nFreqSel = pComboFreq->GetCurSel();
		DWORD dwPulseFreq = 6 - nFreqSel;

		CComboBox* pComboWidth = (CComboBox*)GetDlgItem(IDC_COMBO_STI_WIDTH);
		int nWidthSel = pComboWidth->GetCurSel();
		DWORD dwPulseWidth = nWidthSel + 1;

		m_wbpCurWA.dwPulseFrq	= dwPulseFreq;
		m_wbpCurWA.dwPulseWidth	= dwPulseWidth;
		m_wbpCurWA.dwPulseCount	= dwPulseCount;

 		BOOL bWriteOK = FALSE;
		WRITE_WA_MORE_TIMES(bWriteOK, pBTMan->cmdWriteBasicPara(nWAIndex, m_wbpCurWA));

		if(bWriteOK)
			GYMsgBox(IDS_SYS_WRITE_PARAM_SUCC, MB_ICONINFORMATION);
		else
			GYMsgBox(IDS_SYS_WRITE_PARAM_ERROR, MB_ICONSTOP);
	}
}

void CSystemConfig::OnCheck1() 
{
	BOOL bEnable = FALSE;
	for(int i = 0; i < ARRAY_ELE_COUNT(s_Contrls); i++)
	{
		CButton* pCheck = (CButton*)GetDlgItem(s_Contrls[i].chkID);
		if(pCheck->GetCheck())
		{
			bEnable = TRUE;
			break;
		}
	}

	GetDlgItem(IDC_BUTTON_SAVE_WADATA)->EnableWindow(bEnable);
}

void CSystemConfig::OnCheck2() 
{
	OnCheck1();
}

void CSystemConfig::OnCheck3() 
{
	OnCheck1();
}

void CSystemConfig::OnCheck4() 
{
	OnCheck1();
}

void CSystemConfig::OnCheck5() 
{
	OnCheck1();
}

void CSystemConfig::OnSelchangeComboSysUsers() 
{
	CComboBox* pComboUsers = (CComboBox*)GetDlgItem(IDC_COMBO_SYS_USERS);
	int nSel = pComboUsers->GetCurSel();
	GetDlgItem(IDC_STATIC_SET_REALNAME)->SetWindowText(m_vecUserInfo[nSel].strRealName);

}

void CSystemConfig::OnSelchangeWAList()
{
	CComboBox* pComboWA = (CComboBox*)GetDlgItem(IDC_COMBO_WADEVICES);
	if(pComboWA == NULL)
	{
		ASSERT(pComboWA);
		return;
	}

	int nSel = pComboWA->GetCurSel();
	if(nSel != m_nCurrentWA && pComboWA->GetCount() > 0)
	{
		m_nCurrentWA = nSel;

		CWAManager* pBTMan = GetApp()->GetWAMan();

		int nWAInex = m_vecWAInfo[nSel].nWAIndex;
		m_wbpCurWA = pBTMan->cmdGetBasicPara(nWAInex);
		if(m_wbpCurWA.dwSize == 0)
		{
			GYMsgBox(IDS_SYS_READ_PARAM_ERROR, MB_ICONSTOP);
			return;
		}

		if(m_wbpCurWA.dwPulseFrq < 3 || m_wbpCurWA.dwPulseFrq > 6)
		{
			if(m_wbpCurWA.dwPulseFrq == 0)
				m_wbpCurWA.dwPulseFrq = 1;
			
			CString strFormat = GetResString(IDS_SYS_PULSE_FREQ_WRONG);
			CString strInfo;
			strInfo.Format(strFormat, 100.0/m_wbpCurWA.dwPulseFrq);
			GYMsgBox(strInfo, MB_ICONWARNING);
		}
		
		if(m_wbpCurWA.dwPulseWidth < 1 || m_wbpCurWA.dwPulseWidth > 5)
		{
			CString strFormat = GetResString(IDS_SYS_PULSE_WIDTH_WRONG);
			CString strInfo;
			strInfo.Format(strFormat, m_wbpCurWA.dwPulseWidth * 50);
			GYMsgBox(strInfo, MB_ICONWARNING);
		}

		CComboBox* pComboFreq = (CComboBox*)GetDlgItem(IDC_COMBO_STI_FREQ);
		ASSERT(pComboFreq);
		pComboFreq->SetCurSel(6 - m_wbpCurWA.dwPulseFrq);
		
		CComboBox* pComboWidth = (CComboBox*)GetDlgItem(IDC_COMBO_STI_WIDTH);
		ASSERT(pComboWidth);
		pComboWidth->SetCurSel(m_wbpCurWA.dwPulseWidth - 1);

		CWnd* pStiCount = GetDlgItem(IDC_EDIT_SYS_SAFE_PULSENUM);
		ASSERT(pStiCount);
		CString strParam;
		strParam.Format(_T("%d"), m_wbpCurWA.dwPulseCount);
		pStiCount->SetWindowText(strParam);
	}	
}

void CSystemConfig::EnableSetPassword()
{
	GetDlgItem(IDC_EDIT_SET_OLD_PWD)->EnableWindow(TRUE);
}

void CSystemConfig::DisableBackup()
{
	GetDlgItem(IDC_BUTTON_DATA_BACKUP)->EnableWindow(FALSE);
}

void CSystemConfig::DisableSystemUpgrade()
{
	GetDlgItem(IDC_BUTTON_SYS_UPGRADE)->EnableWindow(FALSE);
}

void CSystemConfig::DisableSetPassword()
{
	GetDlgItem(IDC_COMBO_SYS_USERS)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_SET_OLD_PWD)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_SET_NEW_PWD)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_SET_NEW_CONFIRM)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_SET_PASSWORD)->EnableWindow(FALSE);
}

void CSystemConfig::DisableNewUser()
{
	GetDlgItem(IDC_EDIT_SYS_NEW_USERNAME)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_SYS_NEW_REALNAME)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_SYS_NEW_PASSWORD)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_SYS_NEW_CONFIRM)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_NEW_USER)->EnableWindow(FALSE);
}

void CSystemConfig::DisableSaveWAData()
{
	GetDlgItem(IDC_CHECK1)->EnableWindow(FALSE);
	GetDlgItem(IDC_CHECK2)->EnableWindow(FALSE);
	GetDlgItem(IDC_CHECK3)->EnableWindow(FALSE);
	GetDlgItem(IDC_CHECK4)->EnableWindow(FALSE);
	GetDlgItem(IDC_CHECK5)->EnableWindow(FALSE);
//	GetDlgItem(IDC_PROGRESS_SAVE_WADATA)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_SAVE_WADATA)->EnableWindow(FALSE);
}

void CSystemConfig::DisableSetWAParam()
{
	GetDlgItem(IDC_EDIT_SYS_SAFE_PULSENUM)->EnableWindow(FALSE);
}

void CSystemConfig::DisableSetWAParam4Time()
{
	GetDlgItem(IDC_COMBO_WADEVICES_TIME)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_SYS_UPTIME)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_SYS_DOWNTIME)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_WRITE_WATIME)->EnableWindow(FALSE);
}

void CSystemConfig::DisableSetHospitalName()
{
	GetDlgItem(IDC_EDIT_SYS_HOSPITAL_NAME)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_SET_HOSPITAL_NAME)->EnableWindow(FALSE);
}

void CSystemConfig::SetEditLimit()
{
	((CEdit*)GetDlgItem(IDC_EDIT_SET_OLD_PWD))->SetLimitText(20);
	((CEdit*)GetDlgItem(IDC_EDIT_SET_NEW_PWD))->SetLimitText(20);
	((CEdit*)GetDlgItem(IDC_EDIT_SET_NEW_CONFIRM))->SetLimitText(20);
	
	((CEdit*)GetDlgItem(IDC_EDIT_SYS_NEW_USERNAME))->SetLimitText(9);
#ifdef _UNICODE
	((CEdit*)GetDlgItem(IDC_EDIT_SYS_NEW_REALNAME))->SetLimitText(7);
#else
	((CEdit*)GetDlgItem(IDC_EDIT_SYS_NEW_REALNAME))->SetLimitText(14);
#endif
	((CEdit*)GetDlgItem(IDC_EDIT_SYS_NEW_PASSWORD))->SetLimitText(20);
	((CEdit*)GetDlgItem(IDC_EDIT_SYS_NEW_CONFIRM))->SetLimitText(20);
	
	((CEdit*)GetDlgItem(IDC_EDIT_SYS_SAFE_PULSENUM))->SetLimitText(3);
}

void CSystemConfig::OnButtonDataBackup() 
{
	CTime tCur = CTime::GetCurrentTime();
	CString strDBFileName;
	strDBFileName.Format(_T("%4d-%02d-%02d_%02d%02d%02d"), tCur.GetYear(), tCur.GetMonth(), tCur.GetDay(), tCur.GetHour(), tCur.GetMinute(), tCur.GetSecond());

// 	BOOL bFoundRemovable = FALSE;
// 	TCHAR szRemoableDisk[] = _T("C:\\");
// 
// 	for(TCHAR ch = _T('Z'); ch > _T('C'); ch--)
// 	{
// 		szRemoableDisk[0] = ch;
// 		if(GetDriveType(szRemoableDisk) == DRIVE_REMOVABLE)
// 		{
// 			bFoundRemovable = TRUE;
// 			break;
// 		}
// 	}
// 
// 	if(!bFoundRemovable)
// 	{
// 		if(GYMsgBox(IDS_SYS_BACKUP_NO_REMOVABLE, MB_ICONQUESTION|MB_YESNO) != IDYES)
// 			return;
// 	}
// 
// 	CString strBackupType = GetResString(IDS_SYS_BACKUP_FILETYPE);
// 	CFileDialog savdasFileDlg(FALSE, _T("db"), strDBFileName, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, strBackupType);
// 	savdasFileDlg.m_ofn.lpstrInitialDir= szRemoableDisk;
// 	
// 	if(savdasFileDlg.DoModal() != IDOK)
// 		return;
// 	
// 	CString strDBFile = savdasFileDlg.GetPathName();

	//  Read the backup path, and backup to the path.
	CString strIniFile = GetExePath();
	strIniFile += _T("\\");
	strIniFile += GYWA_INI_FILE;

	TCHAR szBackupPath[MAX_PATH] = {0};
	::GetPrivateProfileString(_T("User"), _T("BackupDir"), _T(""), szBackupPath, MAX_PATH, strIniFile);

	if( !IsFileExist(szBackupPath) )
	{
		::CreateDirectory( szBackupPath , NULL);
	}

	CString strDBFile = szBackupPath;
	strDBFile += _T("\\");
	strDBFile += strDBFileName;
	strDBFile += _T(".db");

	TCHAR szCmdExePath[MAX_PATH] = {0};
	if(!GetEnvironmentVariable(_T("ComSpec"), szCmdExePath, MAX_PATH))
	{
		_tcscpy(szCmdExePath, _T("C:\\Windows\\System32\\cmd.exe"));
	}

	CString strCmd = szCmdExePath;
	strCmd += _T(" /C ");
	strCmd += GetMySQLDumpCmd();
	strCmd += strDBFile;

	PROCESS_INFORMATION	pi = {0};
	STARTUPINFO			si = {0};
	si.cb = sizeof(si);

	TCHAR szCmd[MAX_PATH * 2] = {0};
	_tcscpy(szCmd, strCmd);
	BOOL bRet = ::CreateProcess(szCmdExePath, szCmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
	::WaitForSingleObject(pi.hProcess, INFINITE);

	if(bRet && _taccess(strDBFile, 0) != -1)
	{
		CString strSucc = GetResString(IDS_SYS_DBBACKUP_SUCC);
		strSucc += _T("\r\n\r\n");
		strSucc += strDBFile;

		GYMsgBox(strSucc, MB_ICONINFORMATION);
	}
	else
		GYMsgBox(IDS_SYS_DBBACKUP_FAIL, MB_ICONSTOP);
}

void CSystemConfig::OnButtonSysUpgrade() 
{
	BOOL bFoundRemovable = FALSE;
	TCHAR szRemoableDisk[] = _T("C:\\");
	
	for(TCHAR ch = _T('Z'); ch > _T('C'); ch--)
	{
		szRemoableDisk[0] = ch;
		if(GetDriveType(szRemoableDisk) == DRIVE_REMOVABLE)
		{
			bFoundRemovable = TRUE;
			break;
		}
	}
	
	if(!bFoundRemovable)
	{
		GYMsgBox(IDS_SYS_NOT_FOUND_REMOVABLE, MB_ICONSTOP);
			return;
	}

	TCHAR szCmdExePath[MAX_PATH] = {0};
	if(!GetEnvironmentVariable(_T("ComSpec"), szCmdExePath, MAX_PATH))
	{
		_tcscpy(szCmdExePath, _T("C:\\Windows\\System32\\cmd.exe"));
	}
	
	CTime tCur = CTime::GetCurrentTime();
	CString strDBFile;
	strDBFile.Format(_T("%s%s_%4d-%02d-%02d_%02d%02d%02d.db"), szRemoableDisk, GetApp()->GetSoftwareSN(), tCur.GetYear(), tCur.GetMonth(), tCur.GetDay(), tCur.GetHour(), tCur.GetMinute(), tCur.GetSecond());

	CString strCmd = szCmdExePath;
	strCmd += _T(" /C ");
	strCmd += GetMySQLDumpCmd();
	strCmd += strDBFile;
	
	PROCESS_INFORMATION	pi = {0};
	STARTUPINFO			si = {0};
	si.cb = sizeof(si);

	TCHAR szCmd[MAX_PATH * 2] = {0};
	_tcscpy(szCmd, strCmd);
	BOOL bRet = ::CreateProcess(szCmdExePath, szCmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
	::WaitForSingleObject(pi.hProcess, INFINITE);
	
	if(bRet && _taccess(strDBFile, 0) != -1)
	{
		CString strSQL;
		strSQL.Format(_T("SELECT ComputerNo FROM SysConfig WHERE ConfigName='SoftwareSerialNo' AND ConfigValue='%s'"), GetApp()->GetSoftwareSN());
		CGYDataSet setSysConfig(strSQL);

		CString strComputerNo;
		setSysConfig.GetFieldValue(_T("ComputerNo"), strComputerNo);

		strSQL.Format(_T("SELECT ConfigValue FROM SysConfig WHERE ComputerNo=%s AND ConfigName='VersionNo'"), strComputerNo);
		setSysConfig.OpenTable(strSQL);

		CString strVersion;
		setSysConfig.GetFieldValue(_T("ConfigValue"), strVersion);

		int nDotPos = strVersion.ReverseFind(_T('.'));
		int nVer = _ttoi(strVersion.Mid(nDotPos + 1));
		nVer++;

		strVersion.Format(_T("%s%d"), strVersion.Left(nDotPos + 1), nVer);
		strSQL.Format(_T("UPDATE SysConfig SET ConfigValue='%s' WHERE ComputerNo=%s AND ConfigName='VersionNo'"), strVersion, strComputerNo);
		setSysConfig.ExecuteSQL(strSQL);

		CNavigateDlg* pNavi = GetApp()->GetFrameDlg();
		pNavi->SetVersionNo(strVersion);

		GYMsgBox(IDS_SYS_UPGRADE_SUCC, MB_ICONINFORMATION);
	}
	else
	{
		GYMsgBox(IDS_SYS_DBBACKUP_FAIL, MB_ICONSTOP);
	}
}

void CSystemConfig::OnSelchangeComboWadevicesTime() 
{
	CComboBox* pComboWA4Time = (CComboBox*)GetDlgItem(IDC_COMBO_WADEVICES_TIME);
	ASSERT(pComboWA4Time);
	
	int nSel = pComboWA4Time->GetCurSel();
	if(nSel != m_nCurrentWA4Time && pComboWA4Time->GetCount() > 0)
	{
		m_nCurrentWA4Time = nSel;

		CWAManager* pBTMan = GetApp()->GetWAMan();
		
		int nWAInex = m_vecWAInfo4Time[nSel].nWAIndex;
		m_wbpCurWA4Time = pBTMan->cmdGetBasicPara(nWAInex);
		
		if(m_wbpCurWA4Time.dwSize == 0)
		{
			GYMsgBox(IDS_SYS_READ_PARAM_ERROR, MB_ICONSTOP);
			return;
		}
		
		CString strUpTime;
		strUpTime.Format(_T("%.1f"), m_wbpCurWA4Time.dwStiUpTime / 100.0);
		GetDlgItem(IDC_EDIT_SYS_UPTIME)->SetWindowText(strUpTime);
		
		CString strDownTime;
		strDownTime.Format(_T("%.1f"), m_wbpCurWA4Time.dwStiDownTime / 100.0);
		GetDlgItem(IDC_EDIT_SYS_DOWNTIME)->SetWindowText(strDownTime);	
	}
}

void CSystemConfig::OnButtonWriteWaTime() 
{
	CWaitCursor wait;

	CWAManager* pBTMan = GetApp()->GetWAMan();

	CComboBox* pComboWA4Time = (CComboBox*)GetDlgItem(IDC_COMBO_WADEVICES_TIME);
	ASSERT(pComboWA4Time);
	
	if(pComboWA4Time->GetCount() > 0)
	{
		int nWASel = pComboWA4Time->GetCurSel();
		int nWAIndex = m_vecWAInfo4Time[nWASel].nWAIndex;
		
		CString strUpTime;
		CWnd* pUpTime = GetDlgItem(IDC_EDIT_SYS_UPTIME);
		pUpTime->GetWindowText(strUpTime);
		
		CString strDownTime;
		CWnd* pDownTime = GetDlgItem(IDC_EDIT_SYS_DOWNTIME);
		pDownTime->GetWindowText(strDownTime);
		
		double dUpTime		= _tcstod(strUpTime, NULL);
		double dDownTime	= _tcstod(strDownTime, NULL);

		if(dUpTime > 2.0 || dDownTime > 2.0)
		{
			GYMsgBox(IDS_QRY_TIME_LESS_20, MB_ICONSTOP|MB_OK);
			return;
		}

		if(dUpTime >= dDownTime)
		{
			GYMsgBox(IDS_QRY_UP_LESS_DOWN, MB_ICONSTOP|MB_OK);
			return;
		}

		m_wbpCurWA4Time.dwStiUpTime		= (dUpTime		* 100);
		m_wbpCurWA4Time.dwStiDownTime	= (dDownTime	* 100);

		BOOL bWriteOK = FALSE;
		WRITE_WA_MORE_TIMES(bWriteOK, pBTMan->cmdWriteBasicPara(nWAIndex, m_wbpCurWA4Time));

		if(bWriteOK)
			GYMsgBox(IDS_SYS_WRITE_PARAM_SUCC, MB_ICONINFORMATION);
		else
			GYMsgBox(IDS_SYS_WRITE_PARAM_ERROR, MB_ICONSTOP);
	}	
}

void CSystemConfig::InitWAColorIndex()
{
	CWAManager* pBTMan = GetApp()->GetWAMan();
	
	for(int i = 0; i < ARRAY_ELE_COUNT(s_Contrls); i++)
	{
		CWnd* pColor = GetDlgItem(s_Contrls[i].dwColorID);
		ASSERT(pColor);
		
		int nWAIndex = s_Contrls[i].nDevIndex;
		
		if(s_Contrls[i].bShowColor && nWAIndex >= 0)
		{
			CString strBTAddress = pBTMan->GetWAAddress(nWAIndex);
			
			CString strSQL;
			strSQL.Format(_T("SELECT ColorIndex FROM WAColor WHERE WAAddress='%s'"), strBTAddress);
			
			int nColorIndex = 0;
			CGYDataSet setColorIndex(strSQL);
			if(!setColorIndex.IsEOF())
			{
				CString strColorIndex;
				setColorIndex.GetFieldValue(_T("ColorIndex"), strColorIndex);
				nColorIndex = _ttoi(strColorIndex);
				s_Contrls[i].nColorIndex = nColorIndex;
			}
		}
	}
}


void CSystemConfig::OnButtonSetHospitalName() 
{
	CString strHospitalName;
	GetDlgItem(IDC_EDIT_SYS_HOSPITAL_NAME)->GetWindowText(strHospitalName);

	strHospitalName.TrimLeft();
	strHospitalName.TrimRight();
	if(strHospitalName.IsEmpty())
	{
		GYMsgBox(IDS_SYS_HPT_NAME_EMPTY, MB_ICONSTOP|MB_OK);
		return;
	}

	CString strSQL;
	if(m_strHospitalName.IsEmpty())
	{
		strSQL.Format(_T("INSERT INTO SysConfig (ComputerNo, ConfigName, ConfigValue) VALUES (%s, 'HospitalName', '%s')"), m_strComputerNo, strHospitalName);
	}
	else
	{
		strSQL.Format(_T("UPDATE SysConfig SET ConfigValue='%s' WHERE ComputerNo='%s' AND ConfigName='HospitalName'"), strHospitalName, m_strComputerNo);
	}

	CGYDataSet setSysConfig;
	setSysConfig.ExecuteSQL(strSQL);
	m_strHospitalName = strHospitalName;

	GYMsgBox(IDS_SYS_SET_HPT_NAME_OK, MB_ICONINFORMATION|MB_OK);
}

void CSystemConfig::OnOK()
{
	
}

void CSystemConfig::OnCancel()
{
	
}

HBRUSH CSystemConfig::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

	if(pWnd && nCtlColor == CTLCOLOR_STATIC)
	{
		DWORD dwID = pWnd->GetDlgCtrlID();
		for(int i = 0; i < MAX_SUPPORT_WA_COUNT; i++)
		{
			int nColorIdx = s_Contrls[i].nColorIndex;

			if( dwID == s_Contrls[i].dwColorID		&&
				s_Contrls[i].bShowColor				&&
				nColorIdx >= 0						&&
				nColorIdx < MAX_SUPPORT_WA_COUNT	)
			{
				hbr = m_WAColor[nColorIdx];
				break;
			}
		}
	}
	
	return hbr;
}
